'''
Created on 18 dec. 2011

@author: Daan
'''


import math

from Roads.Road import Road
from Roads.Straight import Straight
from Roads.Turn import Turn
from Roads.Lane import Lane

class BasicMap(object):
    '''
    classdocs
    '''
    def __init__(self, x1, y1, x2, y2, lanes, canvas):
        '''
        Constructor
        '''
        self._x1 = x1
        self._y1 = y1
        self._x2 = x2
        self._y2 = y2
        self._lanes = lanes
        self._canvas = canvas
        
        self._laneStructure = range(0, 3)
        
        self._laneLength = []
        
        offsetX = 0
        offsetY = 0
        
        canvas.create_rectangle((x1, y1, x2, y2), fill="white", outline="")

#        parts = list(None for x in range(4))
        parts = [None, None, None, None]
        
        w = Road.CalcHeight(lanes)
        
        roadX = offsetX + (x1)
        roadY = offsetY + (y1)
        roadL = (y2 - y1)
        
        r4 = Turn(lanes, roadX, roadY, roadL, False, "lt")
        r4.Draw(canvas)
        
        roadX = offsetX + (x1 + ((x2 - x1) - roadL))
        roadY = offsetY + (y1)
        roadL = (y2 - y1)
        
        r2 = Turn(lanes, roadX, roadY, roadL, True, "rt")
        r2.Draw(canvas)
        
        roadX = offsetX + (x1 + (roadL / 2))
        roadY = offsetY + (y1)
        roadL = (x2 - x1) - roadL
        
        r1 = Straight(lanes, roadX, roadY, roadL + 1, True, "ts")
        r1.Draw(canvas)
        
        roadX = roadX
        roadY = offsetY + (y2 - w)
        roadL = roadL
        
        r3 = Straight(lanes, roadX, roadY, roadL + 1, False, "bs")
        r3.Draw(canvas)
        
        parts[0] = r1
        parts[1] = r2
        parts[2] = r3
        parts[3] = r4
        
        genLengths = ((lane, self._CalculateLaneLength(lane, len(parts))) for lane in range(lanes))
        
        for (lane, lengths) in genLengths:
            self._laneLength.append(Lane(lengths, parts))
            for i in range(len(parts)):
                parts[i].AddLength(lane, lengths[i])
    
    def _CalculateLaneLength(self, lane, partCount):
        roadWidth = Road.width
        lengths = list(range(partCount))
        
        #Calculate straight pieces first
        width = self._x2 - self._x1
        height = self._y2 - self._y1
        
        straightLength = (width - height) * 2
        
        lengths[0] = straightLength / 2
        lengths[2] = straightLength / 2
        
        #Calculate circle lengths
        diameter = (self._y2 - self._y1) - (roadWidth * self._lanes * 2)
        diameter += int(math.ceil(roadWidth / 2))
        diameter += roadWidth * (lane - 1)
        
        ringLength = int(math.pi * diameter)
        
        lengths[1] = ringLength / 2
        lengths[3] = ringLength / 2
        
        return lengths
    
#    def _CalculateLaneLength(self, lane, lengths):
#        roadWidth = Road.width
#        
#        #Calculate straight pieces first
#        width = self._x2 - self._x1
#        height = self._y2 - self._y1
#        
#        straightLength = (width - height) * 2
#        
#        lengths[0] = straightLength / 2
#        lengths[2] = straightLength / 2
#        
#        #Calculate circle lengths
#        diameter = (self._y2 - self._y1) - (roadWidth * self._lanes * 2)
#        diameter += int(math.ceil(roadWidth / 2))
#        diameter += roadWidth * (lane - 1)
#        
#        ringLength = int(math.pi * diameter)
#        
#        lengths[1] = ringLength / 2
#        lengths[3] = ringLength / 2
#        
#        return straightLength + ringLength 
    
    def DrawCar(self, lane, pos, car):
        pos = pos % self._laneLength[lane - 1].GetLength()
        
        (part, perc) = self._laneLength[lane - 1].GetPart(pos)
        size = 10
        half = size / 2
        
        if not part == None:
            try:
                (x, y) = part.GetXYInLane(lane, perc, self._canvas)
                
                xy1 = (x - half, y - half)
                xy2 = (xy1[0] + size, xy1[1] + size)
                
                carid = car.GetUIObj() if car.hasUIObj() else car.SetUIObj(self._canvas.create_arc((xy1[0], xy1[1] , xy2[0], xy2[1]), fill="black", outline="", start=0, extent=359))
                
                self._canvas.coords(carid, (xy1[0], xy1[1] , xy2[0], xy2[1]))
            except:
                pass
            
        return pos
        
